// PPI.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <WinSock2.h>
#include "../list/listcnc.h"
#include "../VelocityPlan/velocityplan.h"
#include <Windows.h>
#include <process.h>

#pragma comment(lib, "Ws2_32.lib")

#define INTERNAL_LISTNUM			16
void Queue_Server(void *p)
{
	SOCKET s;
	SOCKET c;
	WSADATA w;

	//
	int error = WSAStartup(0x0202, &w);   // Fill in WSA info
	
	if (error)
	{
		return; //For some reason we couldn't start Winsock
	}

	if (w.wVersion != 0x0202) //Wrong Winsock version?
	{
		WSACleanup();
		return;
	}

	SOCKADDR_IN addr; // The address structure for a TCP socket
	SOCKADDR_IN client_addr;
	int clientaddrlength;
	addr.sin_family = AF_INET;      // Address family
	addr.sin_port = htons(10000);   // Assign port to this socket


	//Accept a connection from any IP using INADDR_ANY
	//You could pass inet_addr("0.0.0.0") instead to accomplish the 
	//same thing. If you want only to watch for a connection from a 
	//specific IP, specify that //instead.
	addr.sin_addr.s_addr = INADDR_ANY;

	s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create socket
	if (s == INVALID_SOCKET)
	{
		return; //Don't continue if we couldn't create a //socket!!
	}

	if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
	{
		//We couldn't bind (this will happen if you try to bind to the same  
		//socket more than once)
		return;
	}

	listen(s, 1);
	c = accept(s, (LPSOCKADDR)&client_addr, &clientaddrlength);
	while (true)
	{
		
		//server gets message first
		recv(c, NULL, 0, 0);

		//Sever sends meessage later
		send(s, NULL, 0, 0);

	}

	closesocket(c);
	closesocket(s);
	WSACleanup();
	
}

void PPI_Run(void *p)
{


	while (1)
	{

		Sleep(1);
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	
	//
	pLIST VPI_internallist = Listcnc_Create(sizeof(LINEAR_DATA), INTERNAL_LISTNUM);

	_beginthread(Queue_Server, 0, VPI_internallist);

	PPI_Run(VPI_internallist);

	return 0;

}

